package com.example.comment.controller;

import com.example.comment.service.CommentService;
import com.example.common.util.R;
import com.example.model.comment.dto.CommentDto;
import com.example.model.comment.dto.CommentLikeDto;
import com.example.model.comment.dto.CommentSaveDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/")
@Tag(name = "评论管理")
@Slf4j
public class CommentController {
    @Autowired
    private CommentService commentService;

    @PostMapping(value = "/comment/addComment")
    @Operation(summary = "增加评论") // bug
    public R addComment(@RequestBody CommentSaveDto commentSaveDto) {
        boolean flag = commentService.saveComment(commentSaveDto);
        if (flag) {
            return R.ok("增加评论成功");
        } else {
            return R.error("增加评论失败");
        }
    }

    @PostMapping(value = "/comment/likeComment")
    @Operation(summary = "点赞评论")
    public R likeComment(@RequestBody CommentLikeDto commentLikeDto) {
        Object object = commentService.likeComment(commentLikeDto);
        if (commentLikeDto.getOperation() == 0) {
            if (object != null) {
                return R.ok("增加点赞评论成功");
            } else {
                return R.error("增加点赞评论失败");
            }
        } else {
            if (object != null) {
                return R.ok("增加取消点赞评论成功");
            } else {
                return R.error("增加取消点赞评论失败");
            }
        }
    }

    @PostMapping(value = "/comment/getByArticleId")
    @Operation(summary = "根据文章编号查询评论") // bug
    public R addComment(@RequestBody CommentDto commentDto) {
        Object object = commentService.getByArticleId(commentDto);
        log.info("object: {}", object);
        if (object != null) {
            return R.ok("文章编号查询评论成功").data("comment", object);
        } else {
            return R.error("文章编号查询评论失败");
        }
    }
}
